##Intragroup_Conflict_Leadership_Study_1a
##Pardon study, hypothetical state governor

##Load data
setwd("C:/Users/lmh735/Dropbox (Political Science)/Partisanship and leadership-Trump/2021 POBE Submission/R&R/Replication Files")
data1.finish<- read.csv("Study_1a.csv", header=TRUE)

##Create data for analyzing partisans pooled, dropping Independents
data1.partisans<- subset(data1.finish, data1.finish$OwnParty!="Independent")

###############################
##Appendix Table C2
##Summary stats and balance tables (partisans only) 
##Balance using chi-squared, exclude independents since excluded in analysis
library(MASS) ##(only run for this, causes issues with tdyr/dplyr, need to detach after)
##gender
balance.gender<- table(data1.partisans$female, data1.partisans$full.treatment.pool.party)
prop.table(balance.gender, 2)
chisq.test(balance.gender)
##education
balance.educ<- table(data1.partisans$educ, data1.partisans$full.treatment.pool.party)
prop.table(balance.educ, 2)
chisq.test(balance.educ)
##income
balance.income<- table(data1.partisans$income, data1.partisans$full.treatment.pool.party)
prop.table(balance.income, 2)
chisq.test(balance.income)
##age (categorical)
balance.age.cat<- table(data1.partisans$age.cat, data1.partisans$full.treatment.pool.party)
prop.table(balance.age.cat, 2)
chisq.test(balance.age.cat)

##demographics for sample
##gender
sample.gender<- table(data1.partisans$female)
prop.table(sample.gender)
##education
sample.educ<- table(data1.partisans$educ)
prop.table(sample.educ)
##income
sample.income<- table(data1.partisans$income)
prop.table(sample.income)
##age (categorical)
sample.age.cat<- table(data1.partisans$age.cat)
prop.table(sample.age.cat)

detach("package:MASS", unload=TRUE)
detach("package:psych", unload=TRUE)

#########################################
##Appendix Table C3
##Summary Stats (post-dissent measures)
summary(data1.partisans$ft_leader2)
length(na.omit(data1.partisans$ft_leader2))
sd(data1.partisans$ft_leader2, na.rm=TRUE)

summary(data1.partisans$vote_L_2)
length(na.omit(data1.partisans$vote_L_2))
sd(data1.partisans$vote_L_2, na.rm=TRUE)

summary(data1.partisans$ft_critic)
length(na.omit(data1.partisans$ft_critic))
sd(data1.partisans$ft_critic, na.rm=TRUE)

summary(data1.partisans$appropriate)
length(na.omit(data1.partisans$appropriate))
sd(data1.partisans$appropriate, na.rm=TRUE)

summary(data1.partisans$vote_crit)
length(na.omit(data1.partisans$vote_crit))
sd(data1.partisans$vote_crit, na.rm=TRUE)

###############################
##Appendix Table C1
##N by condition
library(tidyverse)
study1.N<- data1.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, ft_leader2, Seriousness) %>%
  gather("measure", "value", -party, -ResponseCondition.new, -Seriousness) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new, Seriousness, party) %>%
  summarize(n=n())

study1.N
sum(study1.N$n)

#######################################
##Appendix Table C4
##compare with other crimes (for appendix) - pooling parties
##campaign finance violation
summary(data1.partisans$crime_s_pre_reverse01)
##insurance fraud
summary(data1.partisans$fraud_s_pre_reverse01)
##identity theft
summary(data1.partisans$identity_s_pre_reverse01)
##bribing public officials
summary(data1.partisans$bribe_s_pre_reverse01)
##insider info
summary(data1.partisans$insider_s_pre_reverse01)
##DUI
summary(data1.partisans$dui_s_pre_reverse01)

##By party
##campaign finance violation
t.test(data1.partisans$crime_s_pre_reverse01 ~ data1.partisans$party)
##insurance fraud
t.test(data1.partisans$fraud_s_pre_reverse01 ~ data1.partisans$party)
##identity theft
t.test(data1.partisans$identity_s_pre_reverse01 ~ data1.partisans$party)
##bribing public officials
t.test(data1.partisans$bribe_s_pre_reverse01 ~ data1.partisans$party)
##insider info
t.test(data1.partisans$insider_s_pre_reverse01 ~ data1.partisans$party)
##DUI
t.test(data1.partisans$dui_s_pre_reverse01~ data1.partisans$party)

####################################
##Appendix Table C5
##Perceived seriousness of crime after vignette
##t-test overall 
t.test(data1.partisans$crime_s_post ~ data1.partisans$Seriousness)
##significantly higher in high seriouessness 
##Democrats
t.test(data1.partisans$crime_s_post[data1.partisans$party=="Dem"] ~ data1.partisans$Seriousness[data1.partisans$party=="Dem"])
##Republicans
t.test(data1.partisans$crime_s_post[data1.partisans$party=="GOP"] ~ data1.partisans$Seriousness[data1.partisans$party=="GOP"])

###################################
##Appendix Table C6
##Does seriousness affect evals?
##initial Leader evals
##all partisans
reg.ft_leader1<- lm(ft_leader1 ~ Seriousness,
                    data=data1.partisans)
summary(reg.ft_leader1)

reg.vote_L_1<- lm(vote_L_1 ~ Seriousness,
                  data=data1.partisans)
summary(reg.vote_L_1)

###################################
##Appendic Table C7
##evals after loyalty/dissent 
reg.ft_leader2<- lm(ft_leader2 ~ Seriousness*ResponseCondition,
                    data=data1.partisans)
summary(reg.ft_leader2)

reg.vote_L_2<- lm(vote_L_2 ~ Seriousness*ResponseCondition,
                  data=data1.partisans)
summary(reg.vote_L_2)

reg.ft_critic<- lm(ft_critic ~ Seriousness*ResponseCondition,
                   data=data1.partisans)
summary(reg.ft_critic)

reg.vote_critic<- lm(vote_critic ~ Seriousness*ResponseCondition,
                     data=data1.partisans)
summary(reg.vote_critic)

##output Tables C6 and C7
source("regtable.R")
##set working directory for output
setwd("C:/Users/lmh735/Dropbox (Political Science)/Partisanship and leadership-Trump/2021 POBE Submission/R&R/Replication Files/Output")

outtable.rtf(list("(1) Leader FT"=reg.ft_leader1, "(2) Leader Vote"=reg.vote_L_1),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("SeriousnessLow", "Low Seriousness")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Table C6.rtf")

outtable.rtf(list("(1) Leader FT"=reg.ft_leader2, "(2) Leader Vote"=reg.vote_L_2,
                  "(3) Critic FT" = reg.ft_critic, "(4) Critic Vote"=reg.vote_critic),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("SeriousnessLow", "Low Seriousness"),
                              c("ResponseConditionRebuke", "Dissent"),
                              c("SeriousnessLow:ResponseConditionRebuke", "Dissent x Low")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Table C7.rtf")

#####################################
##Load packages for analyses
##Analysis with dplyr
library(dplyr)
library(tidyr)
library(infer)
library(ggplot2)
library(gridExtra)

##leader affect, post response
study1.resp.all.ft_leader2<- data1.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, ft_leader2) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study1.resp.all.ft_leader2

##plot
pardon_ld_ft<- ggplot(study1.resp.all.ft_leader2) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Governor Feeling Thermometer") +
  ggtitle("(a) Leader Feeling Thermometer")

##t-test
t.test(data1.partisans$ft_leader2 ~ data1.partisans$ResponseCondition)

##partisan differences
party_reg_ft_leader2<- lm(ft_leader2 ~ ResponseCondition*OwnParty,
                    data=data1.partisans)
summary(party_reg_ft_leader2)

###########################################
##leader vote, post response
study1.resp.all.vote_L_2<- data1.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, vote_L_2) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study1.resp.all.vote_L_2

##plot
pardon_ld_vote<- ggplot(study1.resp.all.vote_L_2) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Governor Vote") +
  ggtitle("(b) Leader Vote")

##t-test
t.test(data1.partisans$vote_L_2 ~ data1.partisans$ResponseCondition)

##partisan differences
party_reg_vote_L_2<- lm(vote_L_2 ~ ResponseCondition*OwnParty,
                    data=data1.partisans)
summary(party_reg_vote_L_2)


###########################################
##critic affect, post response
study1.resp.all.ft_critic<- data1.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, ft_critic) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study1.resp.all.ft_critic

##plot
pardon_al_ft<- ggplot(study1.resp.all.ft_critic) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Assembly Leader Feeling Thermometer") +
  ggtitle("(c) Assembly Leader Feeling Thermometer")

##t-test
t.test(data1.partisans$ft_critic ~ data1.partisans$ResponseCondition)

##partisan differences
party_reg_ft_critic<- lm(ft_critic ~ ResponseCondition*OwnParty,
                    data=data1.partisans)
summary(party_reg_ft_critic)

###########################################
##critic vote, post response
study1.resp.all.vote_critic<- data1.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, vote_critic) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study1.resp.all.vote_critic

##plot
pardon_al_vote<- ggplot(study1.resp.all.vote_critic) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Assembly Leader Vote") +
  ggtitle("(d) Assembly Leader Vote")

##t-test
t.test(data1.partisans$vote_critic ~ data1.partisans$ResponseCondition)

##partisan differences
party_reg_vote_critic<- lm(vote_critic ~ ResponseCondition*OwnParty,
                    data=data1.partisans)
summary(party_reg_vote_critic)


#############################################
##Output figures for each study
blank<- ggplot() + theme_void()

##Hypothetical Pardon
grid.arrange(pardon_ld_ft, pardon_ld_vote, pardon_al_ft, pardon_al_vote, nrow=2)
fig.pardon.g<- arrangeGrob(pardon_ld_ft, pardon_ld_vote, pardon_al_ft, pardon_al_vote, nrow=2)
ggsave("Fig2_hyp_pardon_all_DV.png", fig.pardon.g, height=10, width=12)


###########################################
##Mechanisms

##appropriateness of response, post response - mechanisms
##study 1
study1.resp.all.appropriate<- data1.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, appropriate) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study1.resp.all.appropriate

##plot
pardon_approp<- ggplot(study1.resp.all.appropriate) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Appropriateness of Response") +
  ggtitle("(a) Appropropriateness of Assembly Leader Response")

##t-test
t.test(data1.partisans$appropriate ~ data1.partisans$ResponseCondition)

##partisan differences
party_reg_appropriate<- lm(appropriate ~ ResponseCondition*OwnParty,
                    data=data1.partisans)
summary(party_reg_appropriate)


########################################
##who best serves the party histogram
##plot distribution (N) by condition:
study1.resp.all.best.serve.party<- data1.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, best.serve.party.cat) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) 

study1.resp.all.best.serve.party
##set order
study1.resp.all.best.serve.party$value<- factor(study1.resp.all.best.serve.party$value, levels=c("Leader", "Assembly", "Both", "Neither"), ordered=FALSE)

##plot (https://socviz.co/groupfacettx.html)
pardon_best_serves<- ggplot(study1.resp.all.best.serve.party, mapping=(aes(x=value))) + # define aes for each individual component instead
  geom_bar(mapping = aes(y = ..prop.., group = ResponseCondition.new))  + 
  facet_grid(~ ResponseCondition.new) + # split into plots, side by side
  scale_y_continuous(breaks=c(0,.25, .5, .75, 1), limits=c(0,1)) + 
  theme(axis.text=element_text(size=12, angle=90), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Proportion") +
  ggtitle("(b) Who Best Serves the Party")
pardon_best_serves

##calc proportion
study1.resp.all.best.serve.party.prop<- study1.resp.all.best.serve.party %>%
  count(ResponseCondition.new, value, name="n_response_best_serves")
study1.resp.all.best.serve.party.prop
#####loyalty condition
##Leader
study1.resp.all.best.serve.party.prop$n_response_best_serves[1]/sum(study1.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
##Assembly Leader
study1.resp.all.best.serve.party.prop$n_response_best_serves[2]/sum(study1.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
##Both
study1.resp.all.best.serve.party.prop$n_response_best_serves[3]/sum(study1.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
##Neither
study1.resp.all.best.serve.party.prop$n_response_best_serves[4]/sum(study1.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
######dissent condition
##Leader
study1.resp.all.best.serve.party.prop$n_response_best_serves[5]/sum(study1.resp.all.best.serve.party.prop$n_response_best_serves[5:8])
##assembly leader
study1.resp.all.best.serve.party.prop$n_response_best_serves[6]/sum(study1.resp.all.best.serve.party.prop$n_response_best_serves[5:8])
##both
study1.resp.all.best.serve.party.prop$n_response_best_serves[7]/sum(study1.resp.all.best.serve.party.prop$n_response_best_serves[5:8])
##Neither
study1.resp.all.best.serve.party.prop$n_response_best_serves[8]/sum(study1.resp.all.best.serve.party.prop$n_response_best_serves[5:8])


###########################################
##who embarrasses the party histogram
##plot distribution (N) by condition:
study1.resp.all.embarrass.party<- data1.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, embarrass.party.cat) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) 

study1.resp.all.embarrass.party
##set order
study1.resp.all.embarrass.party$value<- factor(study1.resp.all.embarrass.party$value, levels=c("Leader", "Assembly", "Both", "Neither"), ordered=FALSE)

##plot (https://socviz.co/groupfacettx.html)
pardon_embarrass<- ggplot(study1.resp.all.embarrass.party, mapping=(aes(x=value))) + # define aes for each individual component instead
  geom_bar(mapping = aes(y = ..prop.., group = ResponseCondition.new))  + 
  facet_grid(~ ResponseCondition.new) + # split into plots, side by side
  scale_y_continuous(breaks=c(0,.25, .5, .75, 1), limits=c(0,1)) + 
  theme(axis.text=element_text(size=12, angle=90), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Proportion") +
  ggtitle("(c) Who Embarrasses the Party")
pardon_embarrass

##calc proportion
study1.resp.all.embarrass.party.prop<- study1.resp.all.embarrass.party %>%
  count(ResponseCondition.new, value, name="n_response_embarrass")
study1.resp.all.embarrass.party.prop
#####loyalty condition
##Leader
study1.resp.all.embarrass.party.prop$n_response_embarrass[1]/sum(study1.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
##Assembly Leader
study1.resp.all.embarrass.party.prop$n_response_embarrass[2]/sum(study1.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
##Both
study1.resp.all.embarrass.party.prop$n_response_embarrass[3]/sum(study1.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
##Neither
study1.resp.all.embarrass.party.prop$n_response_embarrass[4]/sum(study1.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
######dissent condition
##Leader
study1.resp.all.embarrass.party.prop$n_response_embarrass[5]/sum(study1.resp.all.embarrass.party.prop$n_response_embarrass[5:8])
##assembly leader
study1.resp.all.embarrass.party.prop$n_response_embarrass[6]/sum(study1.resp.all.embarrass.party.prop$n_response_embarrass[5:8])
##both
study1.resp.all.embarrass.party.prop$n_response_embarrass[7]/sum(study1.resp.all.embarrass.party.prop$n_response_embarrass[5:8])
##Neither
study1.resp.all.embarrass.party.prop$n_response_embarrass[8]/sum(study1.resp.all.embarrass.party.prop$n_response_embarrass[5:8])


###########################################
##Plot figures of mechanisms
##Pardon study
grid.arrange(pardon_approp, pardon_best_serves, pardon_embarrass, nrow=1)
pardon_mech<- arrangeGrob(pardon_approp, pardon_best_serves, pardon_embarrass, nrow=1)
ggsave("Fig3_pardon_mechanisms.png", pardon_mech, height=5, width=15)

############################################
##Partisan Differences in Response
##Appendix Table C8

outtable.rtf(list("(1) Leader FT"= party_reg_ft_leader2, "(2) Leader Vote"= party_reg_vote_L_2,
                  "(3) Critic FT" = party_reg_ft_critic, "(4) Critic Vote"= party_reg_vote_critic,
                  "(5) Approp. Response" = party_reg_appropriate),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("ResponseConditionRebuke", "Dissent"),
                              c("OwnPartyRepublican", "Republican"),
                              c("ResponseConditionRebuke:OwnPartyRepublican", "Dissent x Rep")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Table C8.rtf")


######################################################
##              Within subjects analysis            ##
######################################################
##Appendix H, Table H1

##Holding effect of Response constant across stage 1 treatment groups
##Study 1
#Feeling thermometer
study1.ft<- lm(ft_leader2 ~ ResponseCondition + ft_leader1,
               data=data1.partisans)
summary(study1.ft)

#Vote
study1.vote<- lm(vote_L_2 ~ ResponseCondition + vote_L_1,
                 data=data1.partisans)
summary(study1.vote)


#############################################
##              Power calc                 ##
#############################################
##Appendix I, Table I1

##assembly leader ft
##Governor - Loyal
summary(data1.partisans$ft_critic[data1.partisans$ResponseCondition=="Agree"])
sd(data1.partisans$ft_critic[data1.partisans$ResponseCondition=="Agree"])
length(data1.partisans$ft_critic[data1.partisans$ResponseCondition=="Agree"])
##Governor - Dissent
summary(data1.partisans$ft_critic[data1.partisans$ResponseCondition=="Rebuke"])
sd(data1.partisans$ft_critic[data1.partisans$ResponseCondition=="Rebuke"])
length(data1.partisans$ft_critic[data1.partisans$ResponseCondition=="Rebuke"])
##Power analysis
library(stats)
power.t.test(n=362, sig.level=0.05, power=0.80, sd=0.27)
##Note used lower of N in loyal and N in dissent for n, sd is larger sd in loyal and dissent
##could have detected a mean difference of 0.056 (observed difference was 0.00705)
t.test(data1.partisans$ft_critic ~ data1.partisans$ResponseCondition)
0.3163260-0.3092742

##critic vote
##Governor - Loyal
summary(data1.partisans$vote_critic[data1.partisans$ResponseCondition=="Agree"])
sd(data1.partisans$vote_critic[data1.partisans$ResponseCondition=="Agree"])
length(data1.partisans$vote_critic[data1.partisans$ResponseCondition=="Agree"])
##Governor - Dissent
summary(data1.partisans$vote_critic[data1.partisans$ResponseCondition=="Rebuke"])
sd(data1.partisans$vote_critic[data1.partisans$ResponseCondition=="Rebuke"])
length(data1.partisans$vote_critic[data1.partisans$ResponseCondition=="Rebuke"])
##Power analysis
library(stats)
power.t.test(n=362, sig.level=0.05, power=0.80, sd=0.32)
##Note used lower of N in loyal and N in dissent for n, sd is larger sd in loyal and dissent
t.test(data1.partisans$vote_critic ~ data1.partisans$ResponseCondition)
0.5137793 - 0.5008618


